package com.acme.bankapp.domain.bank;

import java.util.Date;

/**
 * Created by anton on 8/18/14.
 */
public class Bank {
    private final int maxClients = 10;
    private int activeClients = 0;
    private Client[] clients = new Client[maxClients];
    private ClientRegistrationListener[] listeners;
    private DebugListener debugListener = null;


    // Constructor
    public Bank(Client[] clients) {
        this.clients = clients;
    }
    public Bank() {
        this.debugListener = new DebugListener();
        this.listeners = new ClientRegistrationListener[]{new EmailNotificationListener(), new PrintClientListener(), };
    }

    public Client[] getClients() {
        return clients;
    }

    public int getMaxClients() {
        return maxClients;
    }

    public void addClient(Client client) {
        if(this.activeClients >= this.maxClients) {
            System.out.println("Bank is full");
        }
        clients[activeClients++] = client;

        // Calling listener
        for(ClientRegistrationListener l : listeners) {
            l.onClientAdded(client);
        }
        debugListener.printInfo(client);
    }

    class DebugListener {
        void printInfo(Client client) {
            System.out.println(client + " " + new Date());
        }
    }

    private class EmailNotificationListener implements ClientRegistrationListener {
        @Override
        public void onClientAdded(Client client) {
            System.out.println("=========================");
            System.out.println("Notification email for client " + client.getName() + " to be sent");
        }
    }

    private class PrintClientListener implements ClientRegistrationListener {
        @Override
        public void onClientAdded(Client client) {
            System.out.println("=================================");
            System.out.println("Information about the client:");
            System.out.println("Name: " + client.getName());
            System.out.println("Gender: " + client.getGender());
            System.out.println("Balance: " + client.getAccount().getBalance());
        }
    }
}
